/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.Encodes;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bus.entity.Address;
import com.thinkgem.jeesite.modules.bus.service.AddressService;
import com.thinkgem.jeesite.modules.bus.service.StatisticService;
import net.sf.excelutils.ExcelException;
import net.sf.excelutils.ExcelUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 统计分析Controller
 * @author sy
 * @version 2019-06-12
 */
@Controller
@RequestMapping(value = "${adminPath}/bus/statistic")
public class StatisticController extends BaseController {

	@Autowired
	private StatisticService statisticService;

	/**
	 * 
	 * @Title: subsidyStatistic
	 * @author: sy
	 * @date: 2019年06月12日 10:49
	 * @Description: 补助统计
	 * @param year
	 * @param request
	 * @param response
	 * @param model
	 * @return: java.lang.String
	 */
	@RequestMapping(value = "subsidyStatistic")
	public String subsidyStatistic(@RequestParam(required=false) String year, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(StringUtils.isBlank(year)){
			year = DateUtils.getYear();
		}
		model.addAttribute("year",year);
		model.addAttribute("dataList",statisticService.subsidyStatistic(year));
		return "modules/bus/subsidyStatistic";
	}

	/**
	 *
	 * @Title: exportSubsidyStatistic
	 * @author: sy
	 * @date: 2019年06月26日 18:06
	 * @Description: 补助统计打印
	 * @param year
	 * @param request
	 * @param response
	 * @return: java.lang.String
	 */
	@RequestMapping(value = "exportSubsidyStatistic")
	public String exportSubsidyStatistic(@RequestParam(required=false) String year, HttpServletRequest request, HttpServletResponse response) throws IOException, ExcelException {
		if(StringUtils.isBlank(year)){
			year = DateUtils.getYear();
		}
		List<Map<String,String>> list = statisticService.subsidyStatistic(year);
		ExcelUtils.addValue("dataRows",list);
		String config = "/template/subsidyStatistic.xls";
		response.reset();
		response.setHeader("Content-disposition", "attachment;filename="+ Encodes.urlEncode("补助统计.xls"));
		response.setContentType("application/vnd.ms-excel");
		ExcelUtils.export(request.getSession().getServletContext(), config, response.getOutputStream());
		return null;

	}

}